package com.yc.myFilterFactory;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 全局过滤器：这里的功能是对所有的请求   计算处理时长
 * @author zwei
 * @create 2024-09-10 10:59
 */
//@Component
@Slf4j
public class MyTimeGatewayFilterFactory implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("进去了：MyTimeGatewayFilterFactory全局过滤");
        long starterTime=System.currentTimeMillis();
        //Promise
        return chain.filter(exchange).then(
                //filter后期处理
                Mono.fromRunnable( ()->{
                    long endTime=System.currentTimeMillis();
                    log.info(exchange.getRequest().getPath()+"请求处理时间："+(endTime-starterTime));
                } )
        );
    }
    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}
